#include "CharacterObject.h"
#include "include.h"

CharacterObject::CharacterObject(ObjectList * objectList,TextureManager * textureManager, Vertex position)
:ClosedObject(textureManager,position)
{
	this->objectList = objectList;
	printf("CharacterObject::CharacterObject\n");
	Vector2D pos = position.GetPosition();
	printf("here 1\n");
	navigationEdgeList = new NavigationEdgeList( new NavigationEdge( textureManager, new Vertex(pos.x + 5, pos.y), 
																	 new Vertex(pos.x - 5, pos.y)));
	printf("here 2\n");
	CreatePropertyMenu();
	printf("here 3\n");
	propertyMenu->SetFormContent(0,"./Skeletons/dinoSkeleton.skl");
	propertyMenu->SetFormContent(1,"1");
	propertyMenu->Hide();
	printf("CharacterObject::CharacterObject Done\n");
}
CharacterObject::CharacterObject(ObjectList * objectList,TextureManager * textureManager, Vertex position,
			      	bool isGrabbable,
			      	char * skeletonFile,
			      	NavigationEdgeList * navigationEdgeList)
:ClosedObject(textureManager,position)
{
	this->objectList = objectList;
	this->navigationEdgeList = navigationEdgeList;
	CreatePropertyMenu();
	char * isGrabbableString = (char*)malloc(sizeof(char)*2);
	isGrabbableString[0] = (int)(isGrabbable) + '0';
	isGrabbableString[1] = '\0';
	propertyMenu->SetFormContent(0,skeletonFile);
	propertyMenu->SetFormContent(1,isGrabbableString);
	propertyMenu->Hide();
}
CharacterObject::~CharacterObject()
{
	if(navigationEdgeList)
	{
		delete(navigationEdgeList);
		navigationEdgeList = NULL;
	}
}

void CharacterObject::AddEdge(NavigationEdge * edge)
{
	if(navigationEdgeList)
	{
		navigationEdgeList->Add(edge);
	}
	else
		navigationEdgeList = new NavigationEdgeList(edge);
	if(objectList)
		objectList->Add(edge);
}
void CharacterObject::RemoveEdge(NavigationEdge * edge)
{
	printf("CharacterObject::RemoveEdge\n");
	if(navigationEdgeList)
		navigationEdgeList->Remove(edge);
	if(objectList)
		objectList->RemoveList(edge);
	printf("Done CharacterObject::RemoveEdge\n");
}

void CharacterObject::AddVertex(Vertex * vertex)
{
}
void CharacterObject::AddVertexBefore(Vertex * vertex, Vertex * before)
{
}
void CharacterObject::RemoveVertex(Vertex * vertex)
{
	if(!navigationEdgeList)
		return;
		
	NavigationEdge * shared = NULL;
	while((shared = navigationEdgeList->VertexInUse(vertex)))
	{
		if(objectList)
			objectList->RemoveList(shared);
			
		navigationEdgeList->Remove(shared);
	}
}
void CharacterObject::GetHoverGroup(Mouse * mouse, VertexList * group)
{
	if((mouse->GetWorldPosition() | position.GetPosition()) < 1)
	{
		printf("HERE2\n");
		group->Add(&position);
		List<Vertex> * search = vertices->head;
		while(search)
		{
			if(search->payload)
			{
				group->Add(search->payload);
			}
			search = search->next;
		}
	}		
}
char * CharacterObject::GetTag()
{
	char * posTag = position.GetTag();
	char * skeletonTag = propertyMenu->GetFormContent(0);
	char * grabTag= propertyMenu->GetFormContent(1);
	char * pathTag = navigationEdgeList->GetTags();
	
	char * tag = (char *)malloc(sizeof(char)*(strlen(posTag)+strlen(skeletonTag) + strlen(grabTag) + strlen(pathTag) + 35));
	
	sprintf(tag, "<character, base, %s, %s, %s, %d, %s>", posTag, skeletonTag, grabTag, navigationEdgeList->GetLength(), pathTag);
	printf("Character Tag = %s\n", tag);
	free(pathTag);
	free(grabTag);
	free(skeletonTag);
	free(posTag);
	return tag;
}
void CharacterObject::CreatePropertyMenu()
{
	printf("CharacterObject::CreatePropertyMenu\n");
	propertyMenu = new FormMenu(textureManager, "2,IsGrabbable,1,SkeletonFile,1", PROPERTY_MENU_POSITION, 0);
	
}
NavigationEdgeList * CharacterObject::GetNavigationEdgeList()
{
	return navigationEdgeList;
}
Vertex * CharacterObject::GetHover(Mouse * mouse)
{
	return NULL;
}